/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import VO.EstadisticasLlamadoVO;
import VO.LlamadoVO;
import java.sql.SQLException;
import utiles.exceptions.DataBaseException;
import utiles.exceptions.InternalErrorException;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.sql.DataSource;
import utiles.SQL.GeneralOperations;
import utiles.SQL.SimpleDataSource;
import utiles.exceptions.DataBaseException;
import utiles.exceptions.InternalErrorException;

/**
 *
 * @author Jorge
 */
public class DaoLlamado implements DaoLlamadoInterface {

    public boolean ingresarLlamado(LlamadoVO nuevo) throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL ingresarLlamado(?,?,?,?,?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("tipoP", nuevo.getTipo());
            callabledStatement.setString("fechaP", nuevo.getFecha());
            callabledStatement.setString("nomSol", nuevo.getNombreSolicitante());
            callabledStatement.setString("fonoSol", nuevo.getFonoSolicitante());
            callabledStatement.setString("idTel", nuevo.getIdTelefonista());

            callabledStatement.execute();


            for (int i = 0; i < nuevo.getVoluntarios().size(); i++) {

                queryString1 = "CALL bitacoraVoluntario(?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("rutP", nuevo.getVoluntarios().get(i).toString());


                callabledStatement.execute();

                

            }

             for (int i = 0; i < nuevo.getMaquinarias().size(); i++) {

                queryString1 = "CALL bitacoraMaquinaria(?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("nom", nuevo.getMaquinarias().get(i).toString());


                callabledStatement.execute();

                System.out.println(nuevo.getMaquinarias().get(i).toString());

            }



            if (callabledStatement.getUpdateCount() > 0) {
                result = true;
            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

    }

   
    public EstadisticasLlamadoVO cargarAno(String ano) throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        EstadisticasLlamadoVO result =null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarEstadisticasLlamados(?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("ano", ano);


            callabledStatement.execute();

            

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst()!=false)
            {
                resultSet.next();
                result = new EstadisticasLlamadoVO();

                result.setNumeroLlamados(resultSet.getInt("llamados"));
                result.setT100(resultSet.getInt("dcero"));
                result.setT101(resultSet.getInt("duno"));
                result.setT102(resultSet.getInt("ddos"));
                result.setT103(resultSet.getInt("dtres"));
                result.setT104(resultSet.getInt("dcuatro"));
                result.setT105(resultSet.getInt("dcinco"));
                result.setT106(resultSet.getInt("dseis"));
                result.setT107(resultSet.getInt("dsiete"));
                result.setT108(resultSet.getInt("docho"));
                result.setT109(resultSet.getInt("dnueve"));
                result.setT1010(resultSet.getInt("ddiez"));
                result.setT1011(resultSet.getInt("donce"));
                result.setT1012(resultSet.getInt("ddoce"));



            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

    }
    public EstadisticasLlamadoVO cargarMes(String ano,String mes) throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        EstadisticasLlamadoVO result =null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarEstadisticasLlamadosMes(?,?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("ano", ano);
            callabledStatement.setString("mes", mes);


            callabledStatement.execute();



            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst()!=false)
            {
                resultSet.next();
                result = new EstadisticasLlamadoVO();

                result.setNumeroLlamados(resultSet.getInt("llamados"));
                result.setT100(resultSet.getInt("dcero"));
                result.setT101(resultSet.getInt("duno"));
                result.setT102(resultSet.getInt("ddos"));
                result.setT103(resultSet.getInt("dtres"));
                result.setT104(resultSet.getInt("dcuatro"));
                result.setT105(resultSet.getInt("dcinco"));
                result.setT106(resultSet.getInt("dseis"));
                result.setT107(resultSet.getInt("dsiete"));
                result.setT108(resultSet.getInt("docho"));
                result.setT109(resultSet.getInt("dnueve"));
                result.setT1010(resultSet.getInt("ddiez"));
                result.setT1011(resultSet.getInt("donce"));
                result.setT1012(resultSet.getInt("ddoce"));



            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

    }
}
